home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / src / mosmllib / test / list.sml < prev    next >
Encoding:
Text File  |  1997-08-18  |  3.4 KB  |  114 lines  |  [TEXT/R*ch]

  1. (* test/list.sml 
  2.    PS 1994-12-10 *)
  3.  
  4. use "auxil.sml";
  5.  
  6. local 
  7.     open List
  8. in
  9. val v123 = [1,2,3];
  10. fun even i = i mod 2 = 0;
  11.  
  12. val test1 = check (null [] andalso not (null [[]]));
  13.  
  14. val test2 = check (1 = hd v123 andalso [2,3] = tl v123 andalso 3 = last v123);
  15.  
  16. val test3 = (hd []   seq "WRONG") handle Empty => "OK" | _ => "WRONG";
  17. val test4 = (tl []   seq "WRONG") handle Empty => "OK" | _ => "WRONG";
  18. val test5 = (last [] seq "WRONG") handle Empty => "OK" | _ => "WRONG";
  19.  
  20. val test6 = check(1 = nth(v123,0) andalso 3 = nth(v123,2));
  21.  
  22. val test7 = (nth(v123,~1) seq "WRONG") handle Subscript => "OK" | _ => "WRONG";
  23. val test8 = (nth(v123,3)  seq "WRONG") handle Subscript => "OK" | _ => "WRONG";
  24.  
  25. val test9 = check (3 = length v123);
  26.  
  27. val test10 = check ([3,2,1] = rev [1,2,3]);
  28.  
  29. val v16 = v123 @ [4,5,6];
  30.  
  31. val test11 = check([1,2,3,4,5,6] = v16);
  32.  
  33. val test12 = check(concat [] = [] andalso concat [v16] = v16 
  34.            andalso concat [v123, [4,5,6]] = v16);
  35.  
  36. val test13 = check(rev v16 = revAppend([4,5,6], [3,2,1]));
  37.  
  38. local 
  39.     val v = ref 0
  40.     fun h [] r = r | h (x::xr) r = h xr (r+r+x): int;
  41.     val isum = h v16 0
  42. in 
  43.     fun reset () = v := 0;
  44.     fun incrv i = v := 2 * !v + i;
  45.     fun checkv () = check(!v = isum);
  46. end;
  47.  
  48. val test14 = (reset (); app incrv v16; checkv);
  49.  
  50. val test15 = check([2,4,6,8,10,12] = map (fn i=>i*2) v16);
  51.  
  52. val test16 = 
  53.     check([3,9,15] = 
  54.       mapPartial (fn i => if even i then NONE else SOME (3*i)) v16);
  55.  
  56. val test17 = check(NONE = find (fn i => i>7) v16);
  57.  
  58. val test18 = check(SOME 5 = find (fn i => i>4) v16);
  59.  
  60. val test19 = check(NONE = find (fn _ => true) []);
  61.  
  62. val test20 = check([2,4,6] = filter even v16);
  63.  
  64. val test21 = (reset (); filter (fn i => (incrv i; true)) v16 seq checkv());
  65.  
  66. val test22 = check(([2,4,6], [1,3,5]) = partition even v16);
  67.  
  68. val test23 = (reset (); partition (fn i => (incrv i; true)) v16 seq checkv());
  69.  
  70. val test24 = check(v16 = foldr op:: [] v16);
  71. val test25 = check(rev v16 = foldl op:: [] v16);
  72.  
  73. val test26 = (reset(); foldr (fn (i,r) => incrv i) () (rev v16); checkv());
  74. val test27 = (reset(); foldl (fn (i,r) => incrv i) () v16; checkv());
  75.  
  76. val test28 = check(21 = foldr op+ 0 v16 andalso 21 = foldl op+ 0 v16);
  77.  
  78. val test29 = check(all (fn _ => false) [] 
  79.            andalso not (exists (fn _ => true) [])); 
  80.  
  81. val test30 = check(exists even [1,1,1,1,1,1,2,1] 
  82.            andalso all even [6,6,6,6,6,6,6,6]);
  83.  
  84. val test31 = check(v16 = tabulate (6, fn i => i+1));
  85.  
  86. val test32 = (reset(); tabulate (6, fn i => (incrv (i+1); 127)) seq checkv());
  87.  
  88. val test33 = check([] = tabulate (0, fn i => 1 div i));
  89.  
  90. val test34 = (tabulate(~1, fn _ => raise Div) seq "WRONG") 
  91.              handle Size => "OK" | _ => "WRONG";
  92.  
  93. val test35a = check(drop([], 0) = [] 
  94.            andalso drop(v123, 0) = v123 
  95.            andalso drop(v123, 3) = []);
  96. val test35b = (drop(v123, ~1) seq "WRONG") 
  97.               handle Subscript => "OK" | _ => "WRONG";
  98. val test35c = (drop(v123, 4) seq "WRONG") 
  99.               handle Subscript => "OK" | _ => "WRONG";
  100.  
  101. val test36a = check(take([], 0) = [] 
  102.            andalso take(v123, 3) = v123
  103.            andalso take(v123, 0) = []);
  104. val test36b = (take(v123, ~1) seq "WRONG") 
  105.               handle Subscript => "OK" | _ => "WRONG";
  106. val test36c = (take(v123, 4) seq "WRONG") 
  107.               handle Subscript => "OK" | _ => "WRONG";
  108.  
  109. val test37a = 
  110.     check'(fn _ => getItem [] = NONE
  111.        andalso getItem [#"A"] = SOME(#"A", [])
  112.        andalso getItem [#"B", #"C"] = SOME(#"B", [#"C"]));
  113. end;
  114.